import * as assetModelService from '../../services/assetModel';
import {queryAssetType} from '../../services/assetType';
export default {
  namespace: 'assetModel',
  state: {
    assetBrandList: [],
    assetModelList: [],
    assetTypeTree: [],
    current : 1,
    currentSize : 10,
    total : 0, 
    pid : 1,  
    modalVisible : false,
    modalType : 'create',
    newKey : '', 
    currentItem:{},
    brandId:1,
    imegeUrl:'',
  },
  reducers: {
     queryAssetTypesSuc(state, { payload: assetTypeTree }) {
      return { ...state, assetTypeTree };
    },
    queryBrandListSuc(state, { payload: assetBrandList }) {
      return { ...state, assetBrandList };
    },
    queryAssetModelSuc(state, { payload }) {
      return { ...state, ...payload };
    },
    showModal (state, { payload }) {
      return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, imegeUrl:'',modalVisible: false, newKey : new Date().getTime() + '' }
    },
    updateState(state, { payload }) {
      return { ...state, ...payload };
    },
  },
  effects: {
    *queryAssetType({ payload },{ call, put }) {
      const { body }  = yield call(queryAssetType);
      yield put({ type: 'queryAssetTypesSuc', payload: body.data });
    },
    *queryAssetBrand({ payload },{ call, put }) {
      const { body } = yield call(assetModelService.queryAssetBrand,payload);
      yield put({ type: 'queryBrandListSuc', payload: body.data });
    }, 
    *queryAssetModel({ payload },{ call, put }) {
      const { body } = yield call(assetModelService.queryAssetModel,payload);
      yield put({ type: 'queryAssetModelSuc', payload: { assetModelList:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords, brandId:payload.brandId }});
    },
    *qryById({ payload },{ call, put }){
       const { body } = yield call(assetModelService.qryById,payload);
        yield put({
          type: 'showModal',
          payload: {
            currentItem: body.data,
            modalType: 'edit',
            imegeUrl:body.data.imgPath?body.data.imgPath:''
          },
       });
    },  

    *create({ payload,search },{ call, put }){
      const { body } = yield call(assetModelService.create,payload);
      yield put({ type: 'queryAssetModel', payload:search});
      yield put({ type: 'hideModal' });
    },
    *edit({ payload,search },{ call, put }){
      const { body } = yield call(assetModelService.edit,payload);
      yield put({ type: 'queryAssetModel', payload:search});
      yield put({ type: 'hideModal' });
    },
    *remove({ payload,search },{ call, put }){
      const { body } = yield call(assetModelService.remove,payload);
      yield put({ type: 'queryAssetModel', payload:search});
    },
  
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/basic/assetModel') {
          dispatch({ type: 'queryAssetType' });
          dispatch({ type: 'queryAssetBrand', payload:{ 'name':'' }});
          dispatch({ type: 'queryAssetModel', payload:{ 'brandId':'' }});
        }
      });
    },
  },
};
